package com.example.xyr.myapplication.utils

import com.example.xyr.myapplication.model.BaseFriendBean

/**
 * Created by ylcf on 2018/9/5.
 */
class PinyinComparator : Comparator<BaseFriendBean> {
    override fun compare(o1: BaseFriendBean?, o2: BaseFriendBean?): Int {
        return sort(o1!!, o2!!)
    }

    private fun sort(lhs: BaseFriendBean, rhs: BaseFriendBean): Int {
        // 获取ascii值
        val lhs_ascii = lhs.piyin.toUpperCase()[0]
        val rhs_ascii = rhs.piyin.toUpperCase()[0]
        // 判断若不是字母，则排在字母之后
        return if (lhs_ascii < 65.toChar() || lhs_ascii > 90.toChar())
            1
        else if (rhs_ascii < 65.toChar() || rhs_ascii > 90.toChar())
            -1
        else
            lhs.piyin.compareTo(rhs.piyin)
    }
}